<script setup lang="ts">
import { getMedicalOrderLogistics } from '@/api/medicine'
import type { Express } from '@/types/medicine'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import endImg from '@/assets/end.png'
import startImg from '@/assets/start.png'
import carImg from '@/assets/car.png'

//导入高德地图依赖
import AMapLoader from '@amap/amap-jsapi-loader'
// 1.获取物流信息
const express = ref<Express>()
const route = useRoute()
onMounted(async () => {
  const { data } = await getMedicalOrderLogistics(route.params.id as string)
  console.log('物流详情', data)

  express.value = data
})
// 2.绘制高德地图
// v2.0 需要配置安全密钥jscode
window._AMapSecurityConfig = {
  securityJsCode: 'd9f3e9a1e718d5bac3e5f150df8ed68c'
}
onMounted(async () => {
  try {
    // 1.写法1:then是回调写法返回promise对象
    // AMapLoader.load({
    //   key: '6315aef4350ff95bfd91c1ae35b30d4c',
    //   version: '2.0'
    // }).then((AMap) => {
    //   // 使用 Amap 初始化地图
    //   console.log('高德地图api对象', AMap)
    // })

    // 2.写法2：async/await
    const AMap = await AMapLoader.load({
      key: '6315aef4350ff95bfd91c1ae35b30d4c',
      version: '2.0'
    })
    console.log('高德地图api对象', AMap)
    const map = new AMap.Map('map', {
      //设置地图容器id
      zoom: 11, //初始化地图级别
      // center: [105.602725, 37.076636] //初始化地图中心点位置
      mapStyle: 'amap://styles/whitesmoke' // 设置地图样式
    })

    // 2.绘制地图轨迹
    //构造路线导航类
    AMap.plugin('AMap.Driving', () => {
      const driving = new AMap.Driving({
        map: map,
        showTraffic: false, // 关闭交通状态
        hideMarkers: true //关闭默认气泡
      })

      // 1.开始位置坐标
      const start = express.value?.logisticsInfo.shift()
      // 2.结束位置坐标
      const end = express.value?.logisticsInfo.pop()
      // 3. 途径点坐标：express.value?.logisticsInfo shift和pop后剩余未途经点坐标，处理成二维数据
      const ways = express.value?.logisticsInfo.map((item) => [item.longitude, item.latitude])
      // 4.自定义气泡
      // 自定义开始结束位置图片
      const startMarker = new AMap.Marker({
        icon: startImg, // 设置自定义图片
        position: [start?.longitude, start?.latitude], // 图片显示的坐标位置
        map: map
      })
      map.add(startMarker)
      const endMarker = new AMap.Marker({
        icon: endImg,
        position: [end?.longitude, end?.latitude],
        map: map
      })
      map.add(endMarker)

      // 根据起终点经纬度规划驾车导航路线
      driving.search(
        [start?.longitude, start?.latitude],
        [end?.longitude, end?.latitude],
        {
          waypoints: ways // 显示途经点坐标
        },
        (status: string, result: object) => {
          // 当前物流位置
          const carMarker = new AMap.Marker({
            icon: carImg,
            position: [
              express.value?.currentLocationInfo.longitude,
              express.value?.currentLocationInfo.latitude
            ],
            anchor: 'center' // 设置基于坐标点显示的位置
          })
          map.add(carMarker)
          // 3s后，定位到货车，放大地图
          setTimeout(() => {
            map.setFitView([carMarker])
            map.setZoom(10)
          }, 3000)
        }
      )
    })
  } catch (err) {
    console.log(err)
  }
})
</script>

<template>
  <div class="order-logistics-page">
    <div id="map">
      <div class="title">
        <van-icon name="arrow-left" @click="$router.back()" />
        <span>{{ express?.statusValue }}</span>
        <van-icon name="service" />
      </div>
      <div class="current">
        <p class="status">{{ express?.statusValue }}——预计{{ express?.estimatedTime }}送达</p>
        <p class="predict">
          <span>{{ express?.name }}</span>
          <span>{{ express?.awbNo }}</span>
        </p>
      </div>
    </div>
    <div class="logistics">
      <p class="title">物流详情</p>
      <van-steps direction="vertical" :active="0">
        <van-step v-for="item in express?.list" :key="item.id">
          <p class="status" v-if="item.statusValue">{{ item.statusValue }}</p>
          <p class="content">{{ item.content }}</p>
          <p class="time">{{ item.createTime }}</p>
        </van-step>
      </van-steps>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.order-logistics-page {
  --van-step-icon-size: 18px;
  --van-step-circle-size: 10px;
}
#map {
  height: 450px;
  background-color: var(--cp-bg);
  overflow: hidden;
  position: relative;
  .title {
    background-color: #fff;
    height: 46px;
    width: 355px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    padding: 0 15px;
    font-size: 16px;
    position: absolute;
    left: 10px;
    top: 10px;
    box-sizing: border-box;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    > span {
      flex: 1;
      text-align: center;
    }
    .van-icon {
      font-size: 18px;
      color: #666;
      &:last-child {
        color: var(--cp-primary);
      }
    }
  }
  .current {
    height: 80px;
    border-radius: 4px;
    background-color: #fff;
    height: 80px;
    width: 355px;
    box-sizing: border-box;
    padding: 15px;
    position: absolute;
    left: 10px;
    bottom: 10px;
    box-shadow: 0px 0px 22px 0px rgba(229, 229, 229, 0.5);
    z-index: 999;
    .status {
      font-size: 15px;
      line-height: 26px;
    }
    .predict {
      color: var(--cp-tip);
      font-size: 13px;
      margin-top: 5px;
      > span {
        padding-right: 10px;
      }
    }
  }
}
.logistics {
  padding: 0 10px 20px;
  .title {
    font-size: 16px;
    padding: 15px 5px 5px;
  }
  .van-steps {
    :deep(.van-step) {
      &::after {
        display: none;
      }
    }
    .status {
      font-size: 15px;
      color: var(--cp-text3);
      margin-bottom: 4px;
    }
    .content {
      font-size: 13px;
      color: var(--cp-tip);
      margin-bottom: 4px;
    }
    .time {
      font-size: 13px;
      color: var(--cp-tag);
    }
  }
}
</style>
